package com.ww.editsms.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.view.WindowManager;

import com.chinawangw.R;

/**
 * 手机系统信息工具类.
 * @since 
 * @version 2013-7-17
 * @author wangwei
 */
public class DeviceUtil extends Application {
	private static int width, height;

	/**
	 * 检查Sdcard.
	 * @param context Context
	 * @return boolean
	 */
	public static boolean checkSdcard(Context context) {
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}

	/**
	 * 获取网络状态.
	 * @param context Context
	 * @return boolean
	 */
	public static boolean checkNet(Context context) {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null) {
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		}
		if (!flag) {
			try {
				MsgDialog.show(context, context.getString(R.string.net_no));
			} catch (Exception e) {
			}
		}
		return flag;
	}

	/**
	 * 
	 * getIMEI(获取设备唯一串号)
	 * 
	 * 
	 * @标题: getIMEI
	 * @描述: 获取设备唯一串号
	 * @param @return 设定文件
	 * @data 2012-07-23-14-37
	 * @return String 设备唯一串号
	 */
	public String getIMEI() {
		TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = "";
		try {
			imei = telephonyManager.getDeviceId().toLowerCase();
		} catch (Exception e) {
			e.printStackTrace();
			imei = "";
		}
		return imei;
	}

	/**
	 * 获取设备品牌
	 */
	@SuppressWarnings("static-access")
	public static String getModel() {
		Build bd = new Build();
		String model = bd.MODEL;
		return model;
	}

	/**
	 * 获取设备号
	 */
	@SuppressWarnings("static-access")
	public static String getDevice() {
		Build bd = new Build();
		String model = bd.DEVICE;
		return model;
	}

	/**
	 * 获取设备号
	 */
	@SuppressWarnings("static-access")
	public static String getBrand() {
		Build bd = new Build();
		String model = bd.BRAND;
		return model;
	}

	/**
	 * 获取手机系统版本
	 */
	public static String getOsType() {
		String model = "Android " + android.os.Build.VERSION.RELEASE;
		return model;
	}

	/**
	 * 判断是哪个运营商(返回string)
	 */
	public String getMobile() {
		String i = "";
		TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		String operator = telManager.getSimOperator();
		if (operator != null) {
			if (operator.equals("46000") || operator.equals("46002")) {
				// 中国移动
				i = "中国移动";
			} else if (operator.equals("46001")) {
				// 中国联通
				i = "中国联通";
			} else if (operator.equals("46003")) {
				// 中国电信
				i = "中国电信";
			}
		}

		return i;
	}

	/**
	 * 判断是哪个运营商（返回int）
	 */
	private static int getMobileForint(Context context) {
		int i = -1;
		TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = telManager.getSimOperator();
		if (operator != null) {
			if (operator.equals("46000") || operator.equals("46002")) {
				// 中国移动
				i = 0;
			} else if (operator.equals("46001")) {
				// 中国联通
				i = 1;
			} else if (operator.equals("46003")) {
				// 中国电信
				i = 2;
			}
		}

		return i;
	}

	/**
	 * 获取网络访问方式.
	 * @param context Context
	 * @return wifi情况下返回'WIFI',其他情况下反馈运营商信息(CMCC,CUCC,CT)
	 */
	public static String getVisitMode(Context context) {
		String visitMode = "CMCC";
		// 获取wifi服务
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		// 判断wifi是否开启
		if (wifiManager.isWifiEnabled()) {
			visitMode = "WIFI";
		} else {
			switch (getMobileForint(context)) {
			case 0:
				visitMode = "CMCC";
				break;
			case 1:
				visitMode = "CUCC";
				break;
			case 2:
				visitMode = "CT";
				break;
			default:
				break;
			}
		}
		return visitMode;
	}

	/**
	 * 检查是否是wifi网络.
	 * @param context Context
	 * @return true:wifi,false;其他网络
	 */
	public static boolean checkWIFI(Context context) {
		// 获取wifi服务
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		// 判断wifi是否开启
		if (wifiManager.isWifiEnabled()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取运营商.
	 * @return  运营商
	 */
	public String getOperator(Context context) {
		String operator = "中国移动";
		switch (getMobileForint(context)) {
		case 0:
			operator = "中国移动";
			break;
		case 1:
			operator = "中国联通";
			break;
		case 2:
			operator = "中国电信";
			break;
		default:
			break;
		}
		return operator;
	}

	/**
	 * 
	 * [功能描述] 获得手机号码
	 * 
	 * @return [参数说明] 手机号码
	 * @createTime 2011-5-3 下午01:48:17
	 */
	public String getTelNum() {
		String telNum = "";
		TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

		try {
			telNum = tm.getLine1Number();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			telNum = "";
		}
		if (telNum != null && telNum.length() > 0 && telNum.equals("null")) {
			telNum = "";
		}
		if (telNum == null) {
			telNum = "";
		}
		return telNum;
	}

	/**
	 * 获取屏幕宽度。
	 * @param context Context
	 * @return 宽度
	 */
	public static int getScreenWidth(Context context) {
		if (width == 0) {
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			width = wm.getDefaultDisplay().getWidth();
		}
		return width;
	}

	/**
	 * 获取屏幕高度。
	 * @param context Context
	 * @return 宽度
	 */
	public static int getScreenHeight(Context context) {
		if (height == 0) {
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			height = wm.getDefaultDisplay().getHeight();
		}
		return height;
	}

	/**
	 * 功能:获得版本号.
	 * @param context Context
	 * @return 版本号
	 * @throws Exception
	 */
	public static String getVersionName(Context context) {
		try {
			// 获取packagemanager的实例
			PackageManager packageManager = context.getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			String version = packInfo.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 功能:获得版本号.
	 * @param context Context
	 * @return 版本号
	 * @throws Exception
	 */
	public static int getVersionCode(Context context) {
		try {
			// 获取packagemanager的实例
			PackageManager packageManager = context.getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			int version = packInfo.versionCode;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 打开应用.
	 * @param context Context
	 * @param packageName 程序包名
	 * @param map  Map<String, String>
	 */
	public static void openApk(Context context, String packageName, Map<String, String> map) {
		PackageManager packageManager = context.getPackageManager();
		Intent intent = new Intent();
		try {
			intent = packageManager.getLaunchIntentForPackage(packageName);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Iterator<Entry<String, String>> ite = map.entrySet().iterator();
		while (ite.hasNext()) {
			Map.Entry<String, String> entry = (Entry<String, String>) ite.next();
			String key = entry.getKey();//map中的key
			String value = entry.getValue();//上面key对应的value
			intent.putExtra(key, value);
		}

		context.startActivity(intent);
	}

	/**
	 * 判断手机已安装某程序.
	 * @param context Context
	 * @param packageName 程序包名
	 * @return 安装：true;未安装:false
	 */
	public static boolean checkApk(Context context, String packageName) {
		final PackageManager packageManager = context.getPackageManager();//获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
		List<String> pName = new ArrayList<String>();//用于存储所有已安装程序的包名
		//从pinfo中将包名字逐一取出，压入pName list中
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				pName.add(pn);
			}
		}
		return pName.contains(packageName);//判断pName中是否有目标程序的包名，有TRUE，没有FALSE
	}

	/**
	 * 判断手机已安装某程序是否需要更新.
	 * @param context Context
	 * @param packageName 程序包名
	 * @param versionCode 程序版本号 
	 * @return 需要：true;不需要:false
	 */
	public static boolean checkApkUpdate(Context context, String packageName, String versionCode) {
		final PackageManager packageManager = context.getPackageManager();//获取packagemanager
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
		//从pinfo中将包名字逐一取出，压入pName list中
		if (pinfo != null) {
			for (int i = 0; i < pinfo.size(); i++) {
				String pn = pinfo.get(i).packageName;
				if (pn.equals(packageName)) {
					int appVersionName = pinfo.get(i).versionCode;
					if (Integer.parseInt(versionCode) > appVersionName) {
						return true;
					}
					break;
				}
			}
		}
		return false;
	}

	/**
	 * 拨打电话.
	 * @param context Context
	 * @param tel 电话号码
	 */
	public static void call(Context context, String tel) {
		if (StringUtils.isNotEmpty(tel)) {
			Uri uri = Uri.parse("tel:" + tel);
			Intent intent = new Intent(Intent.ACTION_DIAL, uri);
			context.startActivity(intent);
		} else {
			MsgDialog.show(context, "空号码");
		}
	}

	/**
	 * 发送短信.
	 * @param context Context
	 * @param tel 电话号码
	 * @param content 内容
	 */
	public static void sms(Context context, String tel, String content) {
		if (StringUtils.isNotEmpty(tel)) {
			Uri smsToUri = Uri.parse("smsto:" + tel);
			Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
			intent.putExtra("sms_body", StringUtils.checkEmpty(content));
			context.startActivity(intent);
		} else {
			MsgDialog.show(context, "空号码");
		}
	}

}